/*
 * This file is part of LangMan.
 *
 * LangMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * LangMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LangMan.  If not, see <http://www.gnu.org/licenses/>.
 */

package langman.gui.tree;

import javafx.beans.Observable;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import langman.business.actions.common.ActionTypes;
import langman.business.actions.defaults.AddEnabled;
import langman.business.data.TranslationService;
import langman.business.data.model.Category;
import langman.gui.ControlSelectionService;
import langman.gui.languagetable.LanguageTable;
import langman.util.doping.Context;
import langman.util.doping.Inject;
import langman.util.doping.SingletonBean;

import java.util.List;

@SuppressWarnings("rawtypes")
@SingletonBean
public class RootItem extends TreeItem implements DecoratingTreeItem, SelectionAwareTreeItem, AddEnabled, FilterableTreeItem {
    private final Context context;
    private final TranslationService translationService;
    private final ControlSelectionService controlSelectionService;

    private final ObservableValue text = new SimpleStringProperty("Translations");

    @Inject
    public RootItem(Context context, TranslationService translationService, ControlSelectionService controlSelectionService) {
        super(translationService);
        this.context = context;
        this.translationService = translationService;
        this.controlSelectionService = controlSelectionService;

        translationService.getCategoriesUnmodifiable().addListener((Observable observable) -> updateChildren());
        updateChildren();
    }

    private void updateChildren() {
        List<Category> categoryList = translationService.getCategoriesUnmodifiable();

        ObservableList children = getChildren();
        children.clear();
        for (Category category : categoryList) {
            CategoryItem categoryItem = context.getBean(CategoryItem.class);
            categoryItem.setCategory(category);
            children.add(categoryItem);
        }
    }

    @Override
    public void selected() {
        controlSelectionService.selectControlLater(LanguageTable.class);
    }

    @Override
    public void decorate(TreeCell cell) {
        cell.textProperty().bind(text);
    }

    @Override
    public ActionTypes getAddAction() {
        return ActionTypes.AddCategory;
    }

    @Override
    public boolean filter(String filterString) {
        List<Category> categoryList = translationService.getCategoriesUnmodifiable();

        ObservableList children = getChildren();
        children.clear();

        for (Category category : categoryList) {
            CategoryItem categoryItem = context.getBean(CategoryItem.class);
            categoryItem.setCategory(category);

            if (categoryItem.filter(filterString)) {
                children.add(categoryItem);
            }
        }


        return true;
    }
}
